﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspMobileBgBindind
{
    public partial class _Default : System.Web.UI.Page
    {
        Dictionary<string, List<string>> ProducersModelsMap = new Dictionary<string, List<string>>();
        List<string> extrasList = new List<string>(){"ABS", "Airbag", "4x4", "DVD", 
                                                     "Alarm","Tiptronic", "Parktronic", "3d Stereo", 
                                                        "Navigation", "Methan", "Autopilot", "Buy Back"};

        string[] engineTypes = new string[3]{ "Diesel", "Gas", "Benzine"};


        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeProducersAndModels();

            if(!IsPostBack)
            {
                this.ProducersList.DataSource = ProducersModelsMap.Keys;
                this.ProducersList.DataBind();

                this.CheckBoxListExtras.DataSource = extrasList;
                this.CheckBoxListExtras.DataBind();

                this.RadioButtonListEngineType.DataSource = engineTypes;
                this.RadioButtonListEngineType.DataBind();
            }
        }

        protected  void InitializeProducersAndModels()
        {
            //string[] producers = new string[] {"BMW", "Opel", "VW", "Aston Martin", "McLaren", "Chevrolet", "Rolls Royce"};            
            ProducersModelsMap.Add("All", new List<string> { });
            ProducersModelsMap.Add("BMW", new List<string>() { "320", "545i", "750", "X5d" });
            ProducersModelsMap.Add("Opel", new List<string>() { "Astra", "Vectra", "Omega", "Speedster" });
            ProducersModelsMap.Add("McLaren", new List<string>() { "F1 GTR", "Mp14-F1", });
            ProducersModelsMap.Add("Porsche", new List<string>() { "911 turbo", "Boxter", "Carera S 2000", "Pannamera", "911 GT1" });
            ProducersModelsMap.Add("Aston Martin", new List<string>() { "Vanquish", "Db9", "Rapide" });
        }

        protected void Page_Prerender(object sender, EventArgs e)
        {
            

        }

        protected void ProducersList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currentProduceer = this.ProducersList.SelectedValue;
            if (currentProduceer == "All")
            {
                foreach (var key in ProducersModelsMap.Keys)
                {
                    ProducersModelsMap[currentProduceer].AddRange(ProducersModelsMap[key]);
                }

            }
            this.ModelsList.DataSource = ProducersModelsMap[currentProduceer];
            this.ModelsList.DataBind();
        }

        protected void ModelsList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ButtonBuildInfo_Click(object sender, EventArgs e)
        {
            string carProducer = ProducersList.SelectedValue;
            string model = ModelsList.SelectedValue;
            string selectedExtras ="Extras: " + "<br />";

            for (int i = 0; i < CheckBoxListExtras.Items.Count; i++)
            {   
                ListItem currItem = CheckBoxListExtras.Items[i];
                if (currItem.Selected)
                {
                    selectedExtras += " &nbsp&nbsp "+ currItem.Text + "<br />";
                }
            }

            LiteralInfo.Text = "Producer: " + carProducer + "<br />" +
                                 "Model: " + model + "<br />" + 
                                 "Engine: " + RadioButtonListEngineType.Text + "<br />" +selectedExtras;

        }
    }
}
